package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.modules.store.entity.TStoreAccountDetailEntity;
import io.renren.modules.store.service.TStoreAccountDetailService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreAccountUnitEntity;
import io.renren.modules.store.service.TStoreAccountUnitService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 21:22:21
 */
@RestController
@RequestMapping("store/tstoreaccountunit")
public class TStoreAccountUnitController extends AbstractController {
    @Autowired
    private TStoreAccountUnitService tStoreAccountUnitService;
    @Autowired
    private TStoreAccountDetailService storeAccountDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstoreaccountunit:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("key",params.get("key"));
        PageUtils page = tStoreAccountUnitService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/checkOut")
    @Transactional(rollbackFor = Exception.class)
    public R checkOut(@RequestBody TStoreAccountUnitEntity storeAccountUnitEntity){
        Double money = storeAccountUnitEntity.getNotSettlementTotal() - storeAccountUnitEntity.getMoney();
        storeAccountUnitEntity.setNotSettlementTotal(money);
        tStoreAccountUnitService.updateById(storeAccountUnitEntity);

        //插入结账明细表
        storeAccountDetailService.save(
                new TStoreAccountDetailEntity().setAccountUnitId(storeAccountUnitEntity.getAccountUnitId())
                .setSettlementMoney(storeAccountUnitEntity.getMoney())
                .setTime(new Date())
                .setPayMethod(storeAccountUnitEntity.getPayMethod())
                .setCreateTime(new Date())
                .setCreateUser(getUser().getCreateUserId())
                .setSettlementLink(storeAccountUnitEntity.getSettlementLink())
                .setSettlementTel(storeAccountUnitEntity.getSettlementTel())
        );
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{accountUnitId}")
    @RequiresPermissions("store:tstoreaccountunit:info")
    public R info(@PathVariable("accountUnitId") Long accountUnitId){
		TStoreAccountUnitEntity tStoreAccountUnit = tStoreAccountUnitService.getById(accountUnitId);

        return R.ok().put("tStoreAccountUnit", tStoreAccountUnit);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstoreaccountunit:save")
    public R save(@RequestBody TStoreAccountUnitEntity tStoreAccountUnit){
        tStoreAccountUnit.setSellerId(getSeller());
		tStoreAccountUnitService.save(tStoreAccountUnit);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstoreaccountunit:update")
    public R update(@RequestBody TStoreAccountUnitEntity tStoreAccountUnit){
		tStoreAccountUnitService.updateById(tStoreAccountUnit);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstoreaccountunit:delete")
    public R delete(@RequestBody Long[] accountUnitIds){
		tStoreAccountUnitService.removeByIds(Arrays.asList(accountUnitIds));

        return R.ok();
    }

}
